package com.example.annolog.thread;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


@Slf4j
public abstract class AbstractCommonThread implements Runnable {

    final static Logger logger = LoggerFactory.getLogger(AbstractCommonThread.class);

    /**
     * 线程类型
     */
    private String threadType;

    public AbstractCommonThread() {
        this.threadType = ThreadTypeEnum.NORMAL.getTypeCode();
    }

    public AbstractCommonThread(String threadType) {
        this.threadType = threadType;
    }

    @Override
    public void run() {
        try {
            executeRun();
        } catch (Exception e) {
            logger.warn("异步线程[{}]执行失败", ThreadTypeEnum.getNameByCode(threadType),
                    e);
        }
    }

    public String getThreadType() {
        return threadType;
    }

    public void setThreadType(String threadType) {
        this.threadType = threadType;
    }

    /**
     * 执行
     */
    protected abstract void executeRun();

    public enum ThreadTypeEnum {
        /**
         * 枚举值
         */
        NORMAL("01", "通用线程");

        /**
         * 线程类型
         */
        private String typeCode;
        /**
         * 线程类型名称
         */
        private String typeName;

        private ThreadTypeEnum(String typeCode, String typeName) {
            this.typeCode = typeCode;
            this.typeName = typeName;
        }

        public String getTypeCode() {
            return typeCode;
        }

        public String getTypeName() {
            return typeName;
        }

        public static String getNameByCode(String typeCode) {
            for (ThreadTypeEnum temp : values()) {
                if (temp.getTypeCode().equals(typeCode)) {
                    return temp.getTypeName();
                }
            }
            return "";
        }
    }


}
